﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sapiens.at.SharePoint.Entities;
using Microsoft.SharePoint;

namespace Sapiens.at.SharePoint.SQLM
{
    public class DummyEntity:Entity
    {
        bool isAsynch = true;
        #region Constructors
        public DummyEntity(int id, string title) : base(id, title) { }
        public DummyEntity(int id, string title, SPList list) : base(id, title, list) { }
        public DummyEntity(SPItemEventProperties eventProperties, SPWeb web)
            : base(eventProperties, web)
        {
            isAsynch = !SQLMatch.isBeforeEvent(eventProperties);
        }
        public DummyEntity(SPListItem listItem) : base(listItem) { }
        public DummyEntity(SPItemEventProperties eventProperties, SPListItem listItem, SPWeb web) : base(eventProperties, listItem, web) { }
        #endregion
        public object GetValue(SPField field)
        {
            var type = field.Type;
            switch (type)
            {
                case SPFieldType.Number:
                case SPFieldType.Currency:
                case SPFieldType.Integer:
                    if (field.Type == SPFieldType.Integer || (field as SPFieldNumber).DisplayFormat == SPNumberFormatTypes.NoDecimal)
                        return Get<int>(field.InternalName);
                    return Get<double>(field.InternalName);
                case SPFieldType.DateTime:
                    return Get<DateTime>(field.InternalName);
                default:
                    if (isAsynch) return ListItem[field.InternalName];
                    return Get<string>(field.InternalName);
            }
        }
        public void SetValue(SPField field, object value)
        {
            if(value is DateTime)
            Set(field.InternalName, (DateTime)value);
            else
            Set(field.InternalName, value);
        }
    }
}
